//
//  AppDelegate.m
//  used
//
//  Created by xalo on 16/7/13.
//  Copyright © 2016年 王凯. All rights reserved.
//

#import "AppDelegate.h"

#define kAppID @"zXHc1KwfdGlf5jJS5N527eaQ-gzGzoHsz"
#define kAppKey @"MYvJM5gI151m0G7r72dOMu1J"
#import "used-Swift.h"
@interface AppDelegate ()<CLLocationManagerDelegate>

/** 用于获取位置 */
@property (nonatomic, strong) CLLocationManager *locationManager;

@property (strong,nonatomic) UIImageView *launchView;


@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   
    // 自动获取位置
    [self setLocation];
    
    [self uMSocialData];
    
    [Bmob registerWithAppKey:@"2954bfcf68e79f24ec1710488c384434"];
    
    self.window = [[UIWindow alloc] init];
    self.window.frame = [UIScreen mainScreen].bounds;
    [self.window makeKeyAndVisible];
    self.window.rootViewController = [[YSLTabBarController alloc] init];
 
    //取消tabBar边框
    [[YSLTabBar appearance] setShadowImage:[[UIImage alloc]init]];
    
    //LeanCloud 即时通讯
    [AVOSCloud setApplicationId:kAppID clientKey:kAppKey];
    [CDChatManager manager].userDelegate = [[UserFactory alloc] init];


    
    
    //调用引导画面
    [GuidePagesView showGuidePagesView];
    

    
    // 显示欢迎界面
    YSLWelcomeView *welcomeV = [YSLWelcomeView weicomeView];
    
    
    // 注意：一定要给界面设置Frame
    welcomeV.frame = self.window.bounds;
    
    
    [self.window addSubview:welcomeV];
 
    return YES;
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    [self.launchView removeFromSuperview];
    
}

- (void)setLocation {
    
    //请求获取位置服务
    _locationManager=[[CLLocationManager alloc]init];
    // 开启定位
    [_locationManager startUpdatingLocation];
    // 定位授权
    [_locationManager requestWhenInUseAuthorization];
    _locationManager.delegate = self;
    // distanceFilter是距离过滤器，为了减少对定位装置的轮询次数，位置的改变不会每次都去通知委托，而是在移动了足够的距离时才通知委托程序
    // 它的单位是米，这里设置为至少移动2000再通知委托处理更新;
    _locationManager.distanceFilter = 5000.0;
    // kCLLocationAccuracyBest:设备使用电池供电时候最高的精度
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}

#pragma mark - <CLLocationManagerDelegate>
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    
    //此处locations存储了持续更新的位置坐标值，取最后一个值为最新位置，如果不想让其持续更新位置，则在此方法中获取到一个值之后让locationManager stopUpdatingLocation
    CLLocation *cl = [locations lastObject];
    NSLog(@"纬度--%f",cl.coordinate.latitude);
    NSLog(@"经度--%f",cl.coordinate.longitude);
    
    // 初始化location
//    CLLocation *location = [[CLLocation alloc] initWithLatitude:cl.coordinate.latitude longitude:cl.coordinate.longitude];
//    
//    // 地理编码 反地理编码
//    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//    // 将经纬度转换成位置
//    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//        NSLog(@"%@", placemarks.firstObject.name);
//    }];
    
//    if (self.block) {
//        self.block();
//    }
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"定位失败");
}


// 友盟
- (void)uMSocialData {

    [UMSocialData setAppKey:@"579186aae0f55a6e91001036"];
    //设置微信AppId、appSecret，分享url
    [UMSocialWechatHandler setWXAppId:@"wx1b0da94db5a3d3fd" appSecret:@"a17cd264b327f1cd2bb3eeaf80c5a1dc" url:nil];
    //设置手机QQ 的AppId，Appkey，和分享URL，需要#import "UMSocialQQHandler.h"
    [UMSocialQQHandler setQQWithAppId:@"1105561010" appKey:@"9yD6VL0rYhHsSOtt" url:nil];
    //打开新浪微博的SSO开关，设置新浪微博回调地址，这里必须要和你在新浪微博后台设置的回调地址一致。需要 #import "UMSocialSinaSSOHandler.h"
    [UMSocialSinaSSOHandler openNewSinaSSOWithAppKey:@"999883566" secret:@"15a7dfb8e458f482120062b52d7775ea"
        RedirectURL:nil];
}

// 系统回调配置，注意如果同时使用微信支付、支付宝等其他需要改写回调代理的SDK，请在if分支下做区分，否则会影响 分享、登录的回调
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    BOOL result = [UMSocialSnsService handleOpenURL:url];
    if (result == FALSE) {
        //调用其他SDK，例如支付宝SDK等
        
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
        }];
        
    }
    return result;
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
